<template>
  <div>
    <el-dialog title="收货地址" :visible="openadd">
      <el-form :model="form" label-width="100px" :rules="rules" ref="form">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" placeholder="请选择部门负责人">
            <el-option
              :label="item.userList"
              :value="item.username"
              v-for="item in userList"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeadd">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentsInfo, addDepartmentApi } from '@/api/departments'
import { getEmployeesList } from '@/api/employees'
export default {
  data () {
    return {
      form: {
        code: '',
        name: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          },
          {
            validator: async (rule, value, callback) => {
              let res = await getDepartmentsInfo()
              res.data.depts.filter(
                item => item.pid === this.currentObj.id && item.name === value
              ).length > 0
                ? callback(new Error('部门名称重复'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '长度在 1 到 50 个字符',
            trigger: 'blur'
          },
          {
            validator: async (rule, value, callback) => {
              let res = await getDepartmentsInfo()
              res.data.depts.some(item => item.code === value)
                ? callback(new Error('部门编码重复'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '长度在 1 到 300 个字符',
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ]
      },
      userList: []
    }
  },
  methods: {
    closeadd () {
      this.$emit('closeadd')
    },
    async getEmployees () {
      let res = await getEmployeesList()
      this.userList = res.data
    },
    submit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          await addDepartmentApi({ ...this.form, pid: this.currentObj.id })
          this.$emit('closeShow')
          this.$message.success('新增成功')
          this.form = {
            code: '',
            name: '',
            manager: '',
            introduce: ''
          }
          this.$emit('updateData')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  created () {
    this.getEmployees()
  },
  props: ['openadd', 'currentObj']
}
</script>

<style></style>
